<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/10/14
 * Time: 14:40
 */

namespace App\Http\Controllers;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Cache;
use App\Tools\WechatPay;


header('Access-Control-Allow-Origin:*');

class UserList extends Controller
{

    //获取openid
    public function getOpenId(){

        if(!isset($_POST['code']) || !$_POST['code']){
            return $this->json('code参数不存在！','',0,404);
        }

        $min_app = config('wechat.app');
        $url = 'https://api.weixin.qq.com/sns/jscode2session?appid='.$min_app['appid'].'&secret='.$min_app['app_secret'].'&js_code='.$_POST['code'].'&grant_type=authorization_code';

        $access_info = $this -> httpGet($url);

        if(isset($access_info['errcode'])){

            return $this->json('code参数错误','',0,301);

        }else{
            if (DB::table('zm_user_list')->where('min_openid', $access_info['openid'])->exists()) {
                $user_id = DB::table('zm_user_list')->where('min_openid', $access_info['openid'])->value('id');
            } else {
                $data['min_openid'] = $access_info['openid'];
                $data['created_at'] = $data['updated_at'] = date("Y-m-d H:i:s");
                $user_id = DB::table('zm_user_list')->insertGetId($data);
            }
            $return['user_id'] = $user_id;
            $return['openid'] = $access_info['openid'];
            Cache::put(env('APP_SHORT_NAME').'_video_user_'.$user_id,$access_info['session_key'],7200);//缓存session_key
            return $this->json('授权成功',$return,1,200);
        }

    }



    //收藏视频添加
    public function addCollect(){

        if(!isset($_POST['user_id']) || !$_POST['user_id'] || !isset($_POST['video_id']) || !$_POST['video_id']){
            return $this->json('参数错误！','',0,404);
        }

        if(DB::table('zm_video_collect')->where(['user_id'=>$_POST['user_id'],'video_id'=>$_POST['video_id']])->exists()){
            $res = true;
        }else{
            $data['video_id'] = $_POST['video_id'];
            $data['user_id'] = $_POST['user_id'];
            $data['created_at'] =  $data['updated_at'] = date('Y-m-d H:i:s');
            $res = DB::table('zm_video_collect')->insertGetId($data);
        }

        if($res){
            return $this->json('收藏成功！','',1,200);
        }else{
            return $this->json('收藏失败！','',0,301);
        }

    }

    //收藏视频删除
    public function delCollect(){

        if(!isset($_POST['user_id']) || !$_POST['user_id'] || !isset($_POST['video_id']) || !$_POST['video_id']){
            return $this->json('参数错误！','',0,404);
        }

        $res = DB::table('zm_video_collect')->where(['user_id'=>$_POST['user_id'],'video_id'=>$_POST['video_id']])->delete();

        if($res){
            return $this->json('取消收藏成功！','',1,200);
        }else{
            return $this->json('取消收藏失败！','',0,301);
        }

    }



    //视频收藏列表
    public function myList(){

        if(!isset($_POST['user_id']) || !$_POST['user_id']){
            return $this->json('参数错误！','',0,404);
        }
        $data = DB::table('zm_video_collect as a')
            ->leftJoin('zm_video_list as b','a.video_id','=','b.id')
            ->where('a.user_id',$_POST['user_id'])
            ->orderBy('id','desc')
            ->select('b.id','b.title','b.img_url','b.share_img','b.video_time','b.view_num','a.created_at')
            ->paginate(10)
            ->toArray();
        $list['current_page'] = $data['current_page'];
        $list['last_page'] = $data['last_page'];
        $list['collect_total'] = $data['total'];
        $list['data'] = $data['data'];
        if($list['data']){
            foreach ($list['data'] as $k=>$v){
                $list['data'][$k]->created_at = date('m-d H:i',strtotime($v->created_at));
            }
        }
        return $this->json('请求成功！',$list,1,200);
    }



    //本地发送GET请求到腾讯并返回结果
    public function httpGet($url) {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url); // CURLOPT_URL 目标 url 地址
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); // CURLOPT_SSL_VERIFYPEER False: 终止 cURL 在服务器进行验证
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // CURLOPT_RETURNTRANSFER 返回原生的（Raw）输出
        $res = curl_exec($curl);
        curl_close($curl);
        return json_decode($res,1);
    }
}
